﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Шаблон элемента пустой страницы задокументирован по адресу http://go.microsoft.com/fwlink/?LinkId=234238

namespace JarvisProject1
{
    public sealed partial class RubricPage : Page
    {
        MainPage obj = new MainPage();
        StoreDataRubric _storeDataRubric = null;
        StoreDataMessage _storeDataMessage = null;

        public RubricPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string parameter = e.Parameter as string;

            _storeDataRubric = new StoreDataRubric();
            List <GroupInfoList <object>> dataLetter = _storeDataRubric.GetGroupsByLetter();
            cvs2.Source = dataLetter;
            (semanticZoom.ZoomedOutView as ListViewBase).ItemsSource = cvs2.View.CollectionGroups;

            //-----------------------------------------------------------------------
            RubricaName.SelectedIndex = Convert.ToInt32(parameter);
            _storeDataMessage = new StoreDataMessage(Convert.ToInt32(parameter));  
            List <GroupInfoList <object>> dataLetter_m = _storeDataMessage.GetGroupsByLetter();
            cvs3.Source = dataLetter_m;
            (semanticZoom2.ZoomedOutView as ListViewBase).ItemsSource = cvs3.View.CollectionGroups;

            base.OnNavigatedTo(e);
        }

        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        private void RubricaName_ItemClick(object sender, ItemClickEventArgs e)
        {
            Messages_in_thisRubrica.Visibility = Visibility.Visible;
            Rubrics.Visibility = Visibility.Collapsed;
            //List_of_rubrics.Visibility = Visibility.Collapsed;
        }

        private void Ready_Click(object sender, RoutedEventArgs e)
        {
            //TO DO: добавление комментария в базу
        }

        private void Back_to_messages_Click(object sender, RoutedEventArgs e)
        {
            Messages_in_thisRubrica.Visibility = Visibility.Visible;
        }

        private void Show_Rubrics_Click(object sender, RoutedEventArgs e)
        {
            DopGrid.Visibility = Visibility.Collapsed;
            But1.Visibility = Visibility.Visible;
            Im1.Visibility = Visibility.Visible;
            Show_Rubrics.Visibility = Visibility.Collapsed;
            Rubrics.Visibility = Visibility.Visible;
            Messages_in_thisRubrica.Visibility = Visibility.Collapsed;
            List_of_rubrics.Visibility = Visibility.Collapsed;
            Add_message.Visibility = Visibility.Collapsed;
            Add_Rubric.Visibility = Visibility.Collapsed;
        }

        private void Previous_Pubrics_Click(object sender, RoutedEventArgs e)
        {
            Messages_in_thisRubrica.Visibility = Visibility.Collapsed;
            Rubrics.Visibility = Visibility.Visible;
            List_of_rubrics.Visibility = Visibility.Collapsed;
        }

        private void Add_messages_Click(object sender, RoutedEventArgs e)
        {

            //Messages_in_thisRubrica.Visibility = Visibility.Collapsed;
        }

        private void Back_to_messages_list_Click(object sender, RoutedEventArgs e)
        {
            Messages_in_thisRubrica.Visibility = Visibility.Visible;
            List_of_rubrics.Visibility = Visibility.Visible;

        }

        private void Minimize_rubrics_Click(object sender, RoutedEventArgs e)
        {
            Rubrics.Width = 785;
            Rubrics.Margin = new Thickness(352, 0, -17, 0);
            Full_rubrics.Visibility = Visibility.Visible;

        }

        private void Full_rubrics_Click(object sender, RoutedEventArgs e)
        {
            Rubrics.Width = 1266;
            Rubrics.Margin = new Thickness(-16, 0, -17, 0);
            Full_rubrics.Visibility = Visibility.Collapsed;
        }

        private void Back_to_rubrics_list_Click(object sender, RoutedEventArgs e)
        {
            List_of_rubrics.Visibility = Visibility.Visible;
            Rubrics.Visibility = Visibility.Visible;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        private void Rubrics_Back_Click(object sender, RoutedEventArgs e)
        {
            DopGrid.Visibility = Visibility.Visible;
            Show_Rubrics.Visibility = Visibility.Visible;
            Rubrics.Visibility = Visibility.Collapsed;
            List_of_rubrics.Visibility = Visibility.Visible;
            Messages_in_thisRubrica.Visibility = Visibility.Visible;
            Add_message.Visibility = Visibility.Visible;
            Add_Rubric.Visibility = Visibility.Visible;
        }

        // При выборе другой рубрики
        private void RubricaName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CounMessage.Text = "Кол-во сообщений: " + obj._object[0]._Rubrics[RubricaName.SelectedIndex].Messsages.Count.ToString();
            NameRubric.Text = obj._object[0]._Rubrics[RubricaName.SelectedIndex].RubrikName;
            _storeDataMessage = new StoreDataMessage(RubricaName.SelectedIndex);
            List<GroupInfoList <object>> dataLetter_m = _storeDataMessage.GetGroupsByLetter();
            cvs3.Source = dataLetter_m;
            (semanticZoom2.ZoomedOutView as ListViewBase).ItemsSource = cvs3.View.CollectionGroups;
        }

        private void ItemsByLetter2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Frame.Navigate(typeof(MessagePage), RubricaName.SelectedIndex.ToString() + "*" + ItemsByLetter2.SelectedIndex.ToString());
        }

        private void ItemsByLetter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DopGrid.Visibility = Visibility.Visible;
            Show_Rubrics.Visibility = Visibility.Visible;
            Rubrics.Visibility = Visibility.Collapsed;
            List_of_rubrics.Visibility = Visibility.Visible;
            Messages_in_thisRubrica.Visibility = Visibility.Visible;
            Add_message.Visibility = Visibility.Visible;
            Add_Rubric.Visibility = Visibility.Visible;
            RubricaName.SelectedIndex = ItemsByLetter.SelectedIndex;
        }

        private void ComboBoxRubric_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < obj._object[0]._Rubrics.Count; i++)
            {
                ComboBoxRubric.Items.Add(obj._object[0]._Rubrics[i].RubrikName);
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            GridAddMessage.Visibility = Visibility.Collapsed;
        }

        private void Add_message_Click_1(object sender, RoutedEventArgs e)
        {
            GridAddMessage.Visibility = Visibility.Visible;
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var dlg = new MessageDialog("Выберите рубрику!");
            var dlg2 = new MessageDialog("Сообщение успешно добавлено!");
            var dlg3 = new MessageDialog("Введите наименование и текст сообщения!");
            dlg2.Commands.Add(new UICommand("OK", new UICommandInvokedHandler((args) =>
            {
                GridAddMessage.Visibility = Visibility.Collapsed;  
            })));

            if (ComboBoxRubric.SelectedIndex == -1)
            { 
                dlg.ShowAsync();
            }
            else if (NameNewMessage.Text == "" && ContentMessageNew.Text == "")
            {
                dlg3.ShowAsync();
            }
            else
            {
                //obj._object[0]._Rubrics[0].Messsages.Add(new ObjectMessage
                //{
                //    Name_Message = NameNewMessage.Text,
                //    Text_of_Message = ContentMessageNew.Text,
                //    ImageAdress = "/Assets/Bloks/2.jpg"
                //});
                dlg2.ShowAsync();
            }
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            GridAddRubric.Visibility = Visibility.Collapsed;
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            var dlg = new MessageDialog("Введите наименование рубрики!");
            var dlg2 = new MessageDialog("Рубрика успешно добавлена!");
            dlg2.Commands.Add(new UICommand("OK", new UICommandInvokedHandler((args) =>
            {
                GridAddRubric.Visibility = Visibility.Collapsed;
            })));

            if (NameNewRubric.Text == "")
            {
                dlg.ShowAsync();
            }
            else
            {
                dlg2.ShowAsync();
            }
        }

        private void Add_Rubric_Click_1(object sender, RoutedEventArgs e)
        {
            GridAddRubric.Visibility = Visibility.Visible;
        }
    }
}
